package com.ruoyi.common.utils.ali;
// Copyright (c) Alibaba, Inc. and its affiliates.

import com.alibaba.dashscope.aigc.imagesynthesis.ImageSynthesis;
import com.alibaba.dashscope.aigc.imagesynthesis.ImageSynthesisParam;
import com.alibaba.dashscope.aigc.imagesynthesis.ImageSynthesisResult;
import com.alibaba.dashscope.exception.ApiException;
import com.alibaba.dashscope.exception.NoApiKeyException;
import com.alibaba.dashscope.utils.JsonUtils;

public class tuya {




        public void asyncCall() {
            System.out.println("---create task----");
            String taskId = this.createAsyncTask();
            System.out.println("---wait task done then return image url----");
            this.waitAsyncTask(taskId);
        }

        /**
         * 创建异步任务
         * @return taskId
         */
        public String createAsyncTask() {
            String prompt = "Flat style illustration of a cow, Generate strictly with reference to the shooting angle and number of cattle in the picture. Colors are bright pastel tones that are soft and inviting. The image has a textured surface with abstract Memphis style accents, making it modern and stylish. High quality output";
            String sketchImageUrl = "https://codingok.oss-cn-beijing.aliyuncs.com/swtp_00636(1).jpg";
            String model = "wanx-sketch-to-image-lite";
            ImageSynthesisParam param = ImageSynthesisParam.builder()
                    .model(model)
                    .prompt(prompt)
                    .n(1)
                    .size("768*768")
                    .sketchImageUrl(sketchImageUrl)
                    .style("<3d cartoon>")

                    .build();

            String task = "image2image";
            ImageSynthesis imageSynthesis = new ImageSynthesis(task);
            ImageSynthesisResult result = null;
            try {
                result = imageSynthesis.asyncCall(param);
            } catch (Exception e){
                throw new RuntimeException(e.getMessage());
            }
            String taskId = result.getOutput().getTaskId();
            System.out.println("taskId=" + taskId);
            return taskId;
        }


        /**
         * 等待异步任务结束
         * @param taskId 任务id
         * */
        public void waitAsyncTask(String taskId) {
            ImageSynthesis imageSynthesis = new ImageSynthesis();
            ImageSynthesisResult result = null;
            try {
                // If you have set the DASHSCOPE_API_KEY in the system environment variable, the apiKey can be null.
                result = imageSynthesis.wait(taskId, null);
            } catch (ApiException | NoApiKeyException e){
                throw new RuntimeException(e.getMessage());
            }

            System.out.println(JsonUtils.toJson(result.getOutput()));
            System.out.println(JsonUtils.toJson(result.getUsage()));
        }


        public static void main(String[] args){
            tuya text2Image = new tuya();
            text2Image.asyncCall();
        }

    }

